package com.nest.WorkNest.act;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ListView;

import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.MSGAdapter;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.HashMap;

public class MyMsgAct extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private ListView msg_list;
    private MSGAdapter adapter;
    private ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
    private Context context = MyMsgAct.this;
    private SwipeRefreshLayout mSwipeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_mymsg);
        initBar();
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("我的消息");
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        tabBack();
        initView();
        initData();
    }

    private void initView() {
        msg_list = (ListView) findViewById(R.id.lv_msg);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.rl_modulename_refresh);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        adapter = new MSGAdapter(this, dataList);
        msg_list.setAdapter(adapter);
    }

    private void initData() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                Logger.json(result);
                if (Tool.getString(result, "resultCode").equals("0")) {
                    dataList = Tool.getListMapByJsonArray(Tool.getJsonArray(result, "message"));
                    adapter.update(dataList);
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostgetMymessage(context, ioAuthCallBack);
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeLayout.setRefreshing(false);
                initData();
            }
        }, 2000);
    }
}
